package com.example.tellwin.home.act;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.adpater.HuoDongListAdapter;
import com.example.tellwin.home.bean.ActivitiesBean;
import com.example.tellwin.home.contract.ActivityListContract;
import com.example.tellwin.home.presenter.ActivityListPresenter;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 活动列表
 */
public class HuoDongListActivity extends CpBaseActivty implements ActivityListContract.View {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.act_title_tv)
    TextView actTitleTv;
    @BindView(R.id.huo_dong_rv)
    RecyclerView huoDongRv;

    List<ActivitiesBean> activitiesBeans = new ArrayList<>();
    HuoDongListAdapter huoDongListAdapter;

    @Inject
    ActivityListPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hd_list);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        huoDongRv.setLayoutManager(linearLayoutManager);
        huoDongRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if (position == 0) {
                    outRect.top = getResources().getDimensionPixelOffset(R.dimen.dp_15);
                }
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_15);
            }
        });
        huoDongListAdapter = new HuoDongListAdapter();
        huoDongRv.setAdapter(huoDongListAdapter);
    }

    @Override
    protected void initEvents() {
        huoDongListAdapter.setOnItemClickListener((position, activitiesBean) -> {
            Intent intent = new Intent(HuoDongListActivity.this, ActDetailActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Common.ACTIVITY_ID, activitiesBean.getActivityId());
            intent.putExtras(bundle);
            HuoDongListActivity.this.startActivity(intent);

        });
        huoDongRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LogUtils.d(CommonUtils.isSlideToBottom(huoDongRv));
                if(CommonUtils.isSlideToBottom(huoDongRv)) {
                    mPresenter.getActiviteList();
                }
            }
        });
    }

    @Override
    protected void initDatas() {
        setTitle(R.string.act_list);
        mPresenter.getActiviteList();

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
    }

    @Override
    public void activiteListResult(List<ActivitiesBean> activitiesBeans) {
        this.activitiesBeans.addAll(activitiesBeans);
        huoDongListAdapter.setBeans(this.activitiesBeans);
    }
}
